<?php

namespace App\Console\Commands;

use App\Http\Models\Mysql\SsyJstPurchaseinOrderTable;
use App\Http\Models\Mysql\SsyPricingExamineTable;
use App\Http\Models\Mysql\SsyProcessingListTable;
use App\Http\Models\Mysql\VerifyPriceTable;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;

class pullPricingExamine extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:pullPricingExamine {--processing_code=}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '拉取加工单核价审核';

    /**
     * Create a new command instance.
     * * /3 * * *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    public function handle()
    {
        $autoKey = "auto_pullPricingExamine";
        Cache::put($autoKey, 1, Carbon::now()->addMinutes(15));
        $conn = (new SsyJstPurchaseinOrderTable())->where('wh_id', 3);
        $processingCode = $this->option("processing_code") ?? "";
        $processingCodes = !empty($processingCode) ? explode(",", $processingCode) : [];
        if(!empty($processingCodes)){
            $conn = $conn->whereIn('processing_code', $processingCodes);
        }else{
            //获取近24小时的回货加工单，同步加工单核价审核
            $start = date('Y-m-d H:i:s', strtotime("- 24 hours"));
            $end = date('Y-m-d H:i:s');
            $conn = $conn->whereBetween('io_date', [$start, $end]);
        }
        $processingList = $conn->groupBy(['processing_code'])->get(['processing_code'])->toArray();
        $total = count($processingList);
        $num = 1;
        if(empty($processingList)){
            Cache::forget($autoKey);
            die("没有需要处理的加工单\n");
        }
        foreach ($processingList as $item) {
            $processingObj = (new SsyProcessingListTable())->where('processing_code', $item['processing_code'])->first();
            $processingObj = !empty($processingObj) ? $processingObj->toArray() : [];
            if(empty($processingObj)){
                echo "共[$total, $num], 没有找到对应的加工单[{$item['processing_code']}]\n";
                $num++;
                continue;
            }
            $verifyPriceObj = (new VerifyPriceTable())->where('number', $processingObj['verify_price_number'])->first();
            $verifyPriceObj = !empty($verifyPriceObj) ? $verifyPriceObj->toArray() : [];
            if(empty($verifyPriceObj)){
                echo "共[$total, $num], 没有找到对应的大货核价[{$item['processing_code']}, ". ($processingObj['verify_price_number'] ?? "") ."]\n";
                $num++;
                continue;
            }
            $exist = (new SsyPricingExamineTable())->where('processing_code', $item['processing_code'])->first();
            $isAdd = true;
            $isvalid = false;
            if(empty($exist)){
                $save = [
                    "factory_contract_no"=>$processingObj['factory_contract_no'],
                    "type_no"=>$processingObj['type_no'],
                    "supplier_id"=>$processingObj['supplier_id'],
                    "factory_name"=>$processingObj['factory_name'],
                    "processing_code"=>$processingObj['processing_code'],
                    "big_pricing_number"=>$processingObj['verify_price_number'], //核价编码
                    "big_pricing_price" => $verifyPriceObj['f67'] ?? 0, //成本加价总价
                    "process_pricing_price" => $verifyPriceObj['f65'] ?? 0,//成衣成本加价总价
                    "order_processor" => $processingObj['order_processor'] ?? "",
                    "createtime"=>date("Y-m-d H:i:s")
                ];
                (new SsyPricingExamineTable())->createData($save);
            }else{
                $isAdd = false;
                if($exist->status != 2){
                    $isvalid = true;
                    $save = [
                        "factory_contract_no" => $processingObj['factory_contract_no'],//合同
                        "big_pricing_number" => $processingObj['verify_price_number'],//大货核价编码
                        "big_pricing_price" => $verifyPriceObj['f67'] ?? 0, //成本加价总价
                        "process_pricing_price" => $verifyPriceObj['f65'] ?? 0,//成衣成本加价总价
                    ];
                    (new SsyPricingExamineTable())->updateData($exist->id, $save);
                }
            }
            $isAddMsg = $isAdd ? "新创建" : (!$isvalid ? "审核通过--无需处理" : "更新");
            echo "共[$total, $num], 正在处理[$isAddMsg]：{$item['processing_code']} {$processingObj['type_no']} 大货核价编码：{$processingObj['verify_price_number']}\n";
            $num++;
        }
        Cache::forget($autoKey);
    }
}
